clear


use "data/data.dta", replace


// Data management 
if 1 {

	
	replace bayesProb = bayesProb / 100
	
	drop bayesPost
	gen bayesPost = bayesProb
	
	drop letterSeenUniqueUp letterSeenUniqueDown letterSeenUniqueLeft letterSeenUniqueRight letterSeenUp letterSeenLeft letterSeenRight

	order letterSeen letterSeenUnique letterSeenUniqueAll bayesProb time timeUnique timeUniqueAll
	
	order id round order
	sort id round 
	
	replace bayesProb = priorNum/100 if id <= 28 & letterSeenUnique == ""
	
	gen betCE = minCE*bet

	
	
	gen condition = .
	replace condition = 1  if known == 1 & catastropheTr == 1 & prior == "low"  & delayTr == 1
	replace condition = 2  if known == 1 & catastropheTr == 1 & prior == "mid"  & delayTr == 1
	replace condition = 3  if known == 1 & catastropheTr == 1 & prior == "high" & delayTr == 1
	replace condition = 4  if known == 1 & catastropheTr == 0 & prior == "low"  & delayTr == 1
	replace condition = 5  if known == 1 & catastropheTr == 0 & prior == "mid"  & delayTr == 1
	replace condition = 6  if known == 1 & catastropheTr == 0 & prior == "high" & delayTr == 1
	replace condition = 7  if known == 0 & catastropheTr == 1 & prior == "low"  & delayTr == 1
	replace condition = 8  if known == 0 & catastropheTr == 1 & prior == "mid"  & delayTr == 1
	replace condition = 9  if known == 0 & catastropheTr == 1 & prior == "high" & delayTr == 1
	replace condition = 10 if known == 0 & catastropheTr == 0 & prior == "low"  & delayTr == 1
	replace condition = 11 if known == 0 & catastropheTr == 0 & prior == "mid"  & delayTr == 1
	replace condition = 12 if known == 0 & catastropheTr == 0 & prior == "high" & delayTr == 1
	
	replace condition = 13   if known == 1 & catastropheTr == 1 & prior == "low"  & delayTr == 0
	replace condition = 14   if known == 1 & catastropheTr == 1 & prior == "mid"  & delayTr == 0
	replace condition = 15   if known == 1 & catastropheTr == 1 & prior == "high" & delayTr == 0
	replace condition = 16   if known == 1 & catastropheTr == 0 & prior == "low"  & delayTr == 0
	replace condition = 17   if known == 1 & catastropheTr == 0 & prior == "mid"  & delayTr == 0
	replace condition = 18   if known == 1 & catastropheTr == 0 & prior == "high" & delayTr == 0
	replace condition = 19   if known == 0 & catastropheTr == 1 & prior == "low"  & delayTr == 0
	replace condition = 20   if known == 0 & catastropheTr == 1 & prior == "mid"  & delayTr == 0
	replace condition = 21   if known == 0 & catastropheTr == 1 & prior == "high" & delayTr == 0
	replace condition = 22   if known == 0 & catastropheTr == 0 & prior == "low"  & delayTr == 0
	replace condition = 23   if known == 0 & catastropheTr == 0 & prior == "mid"  & delayTr == 0
	replace condition = 24   if known == 0 & catastropheTr == 0 & prior == "high" & delayTr == 0

	
	la de cond 	 1 "known, cat, delay, low " ///
				 2 "known, cat, delay, mid " ///
				 3 "known, cat, delay, high" ///
				 4 "known, noCat, delay, low " ///
				 5 "known, noCat, delay, mid " ///
				 6 "known, noCat, delay, high" ///
				 7 "unknown, cat, delay, low " ///
				 8 "unknown, cat, delay, mid " ///
				 9 "unknown, cat, delay, high" ///
				10 "unknown, noCat, delay, low " ///
				11 "unknown, noCat, delay, mid " ///
				12 "unknown, noCat, delay, high" ///
				13 "known, cat, noDelay, low " ///
				14 "known, cat, noDelay, mid " ///
				15 "known, cat, noDelay, high" ///
				16 "known, noCat, noDelay, low " ///
				17 "known, noCat, noDelay, mid " ///
				18 "known, noCat, noDelay, high" ///
				19 "unknown, cat, noDelay, low " ///
				20 "unknown, cat, noDelay, mid " ///
				21 "unknown, cat, noDelay, high" ///
				22 "unknown, noCat, noDelay, low " ///
				23 "unknown, noCat, noDelay, mid " ///
				24 "unknown, noCat, noDelay, high" 
	
	la val cond cond
	
	
	gen cond2 = condition
	replace cond2 = condition - 6 if condition >= 7 & condition <= 12
	replace cond2 = condition - 6 if condition >= 19 & condition <= 24
	
		cap gen winAmt = win
		cap gen weight = . 
		forvalues condition = 1 / 24 {
			foreach winAmt in 0.5 1 5 5.5 {
				local conditionInner = "condition == `condition' & winAmt == `winAmt'"
				
				su priorNum if `conditionInner'
				local prior = r(mean) / 100
				
			
				su state 							if `conditionInner'
				di `prior'
				di r(mean)
				di `winAmt'
				replace weight = `prior' / r(mean)				if state == 1 & `conditionInner' 
				replace weight = (1- `prior') 	/ (1 - r(mean)) if state == 0 & `conditionInner' 

			}
		}
			
		
		gen win_known = win * known
		
		cap gen loss = min(betCE, 0)
		cap gen gain = max(betCE, 0)
		replace loss = . if betCE == .
		replace gain = . if betCE == .
		
		
		gen diffGB = numG - numB
	
}


// Mentioned in flow text
if 1 {
	// Signal length
	su signalLength if win < 1 & known == 1, d
	su signalLength if win > 5 & known == 1, d
	
	reg signalLength highInc if known == 1, cl(id)

	
	
	// Information biasing
	
	
	su diffGB if highInc == 0 & known == 1, d
	su diffGB if highInc == 1 & known == 1, d

	su diffGB if bet == 0 & highInc == 0 & known == 1, d
	su diffGB if bet == 1 & highInc == 0 & known == 1, d
	su diffGB if bet == 0 & highInc == 1 & known == 1, d
	su diffGB if bet == 1 & highInc == 1 & known == 1, d

	reg diffGB highInc if known == 1 & bet == 0, cl(id)
	reg diffGB highInc if known == 1 & bet == 1, cl(id)

	reg diffGB highInc known highInc_known if bet == 0, cl(id)
	reg diffGB highInc known highInc_known if bet == 1, cl(id)
	
	

}


// Supplementary Figure C.2.A: Information biasing
if 1 {
	
	preserve
		
		count if diffGB ~= . 
		count if diffGB ~= . & tag == 1 & bet ~= .
		
		reg diffGB if bet == 0 & highInc == 0 & known == 1, cl(id)
		local s00 = _se[_cons]
		reg diffGB if bet == 1 & highInc == 0 & known == 1, cl(id)
		local s10 = _se[_cons]
		reg diffGB if bet == 0 & highInc == 1 & known == 1, cl(id)
		local s01 = _se[_cons]
		reg diffGB if bet == 1 & highInc == 1 & known == 1, cl(id)
		local s11 = _se[_cons]
		
		
		collapse diffGB, by(highInc known bet)
	
		gen se = .
		replace se = `s00' if bet == 0 & highInc == 0
		replace se = `s10' if bet == 1 & highInc == 0
		replace se = `s01' if bet == 0 & highInc == 1
		replace se = `s11' if bet == 1 & highInc == 1
		
		gen upper = diffGB + 1.96*se
		gen lower = diffGB - 1.96*se

		// Customize graph scheme
		grstyle init
		grstyle set graphsize 64mm 88mm
		grstyle set margin "5mm 5mm 2.5mm 2.5mm": graph
		
		tw 	(line diffGB highInc if bet == 1 & known == 1, lcolor(navy)) ///
			(line diffGB highInc if bet == 0 & known == 1, lcolor(maroon) lpattern(longdash)) ///
			(scatter diffGB highInc if bet == 1 & known == 1, mcolor(navy) msymbol(D)) ///
			(scatter diffGB highInc if bet == 0 & known == 1, mcolor(maroon) msymbol(Oh)) ///
			(rspike lower upper highInc if bet == 1 & known == 1, lcolor(navy) ) ///
			(rspike lower upper highInc if bet == 0 & known == 1, lcolor(maroon) ) ///
			, ///
			xlabel(0 "{&le} $1" 1 "{&ge} $5") ///
			xscale(range(-0.3 1.3)) /// 
			xtitle("Incentive amount") ///
			ylabel(, angle(90)) ///
			ytitle("Difference in number of" "Gs and Bs seen at decision") ///
			legend(order(1 "Accepted" 2 "Rejected") col(1) ring(0) pos(3) region(lc(black))) ///
			bgcolor(white) graphregion(color(white)) ///
			scale(1.3)
		graph export "../writeup/graphs/SupplementaryFigureC2A.pdf", replace	
		export excel "../writeup/sourceDataFigures/sourceData_SupplementaryFigureC2.xlsx", firstrow(variables) sheet("FigureC2A", replace)
		
		// Clear customized graph scheme
		grstyle clear
			
	restore
	
	
}


// Supplementary Figure: Signal length (UNUSED)
if 1 {

	preserve
		collapse signalLength , by(win known )
		
		// Customize graph scheme
		grstyle init
		grstyle set graphsize 64mm 88mm
		grstyle set margin "5mm 5mm 2.5mm 2.5mm": graph
		
		tw	(line signalLength win if known == 1, lcolor(navy) lwidth(0.5) lpattern(solid)) ///
			(line signalLength win if known == 0, lcolor(maroon) lwidth(0.5) lpattern(longdash)) /// 
			, ///
			ytitle("Mean signal length") ///
			xtitle("Incentive amount") ///
			bgcolor(white) graphregion(color(white)) ///
			xlabel(0.5 1 5 5.5) ///
			ylabel(, angle(90)) ///
			xscale(range(0 6)) ///
			legend(order(1 "Incentive First" 2 "Info Choice First") ring(0) pos(10) region(lc(black))) ///
			scale(1.3)
		graph export "../writeup/graphs/signalLength.pdf", replace
		
		// Clear customized graph scheme
		grstyle clear
		
	restore		

}


// Supplementary Figure C.2.B: Welfare CDF Experiment 3
if 1 {
	preserve
		keep if catastropheTreatment == 1 // & prior == "mid" & catastropheTreatment == 0

		cap cumul betCE if known == 1 & highInc == 1 [aw = weight], gen(diffHighTr)
		cap cumul betCE if known == 1 & highInc == 0 [aw = weight], gen(diffLowTr)
		
		sort diffHighTr diffLowTr 
		
		// Customize graph scheme
		grstyle init
		grstyle set graphsize 64mm 88mm
		grstyle set margin "5mm 5mm 2.5mm 2.5mm": graph
					
		tw	(line diffHighTr betCE, lwidth(0.5) lcolor(navy) 	lpattern(solid)) ///
			(line diffLowTr  betCE, lwidth(0.5) lcolor(maroon) 	lpattern(shortdash)) ///
			, ///
			xtitle("Welfare") ///
			xlabel(-6(6)6) /// 
			ytitle("Cumulative frequency") ///
			ylabel(, angle(90)) ///
			legend(order(1 "High incentive" 2 "Low incentive") ring(0) col(1) pos(10) region(lc(black))) ///		
			xline(0, lcolor(black) lpattern(solid) lwidth(0.25)) ///
			bgcolor(white) graphregion(color(white)) ///
			scale(1.3)
		graph export "../writeup/graphs/SupplementaryFigureC2B.pdf", replace
		keep diffHighTr diffLowTr betCE
		export excel "../writeup/sourceDataFigures/sourceData_SupplementaryFigureC2.xlsx", firstrow(variables) sheet("FigureC2B", modify)
		
		// Clear customized graph scheme
		grstyle clear
		
	restore
}		

		
// Supplementary Table C.1: Welfare table linear gains / losses 
if 1 {
	
	cap drop winMin
	gen winMin = win - 1
	replace winMin = . if win == 0
	
	set seed 1
	
	mysureg (gain winMin) /// 
			(loss winMin) ///
			if known == 1 & catastrophe == 0 ///
			, vce(cluster id)

	local mMin = 0
	noisily: nlcom   (eq1:(([gain]_cons + `mMin'*[gain]win) / (([gain]_cons + `mMin'*[gain]win) - ([loss]_cons +  `mMin'*[loss]win))) ///
			- ([gain]win / ([gain]win - [loss]win))) ///
			(eq2:([gain]win / ([gain]win - [loss]win))) ///						
			, post
	

	mysureg (gain winMin) /// 
			(loss winMin) ///
			if known == 1 & catastrophe == 1 ///
			, vce(cluster id)			
	
	set seed 1
	
	
			
	mysureg (gain winMin) /// 
			(loss winMin) ///
			if known == 1 ///
			, vce(cluster id)

	local mMin = 0
	noisily: nlcom   (eq1:(([gain]_cons + `mMin'*[gain]win) / (([gain]_cons + `mMin'*[gain]win) - ([loss]_cons +  `mMin'*[loss]win))) ///
			- ([gain]win / ([gain]win - [loss]win))) ///
			(eq2:([gain]win / ([gain]win - [loss]win))) ///						
			, post
			

			
			
	mysureg (gain winMin) /// 
			(loss winMin) ///
			if known == 0 ///
			, vce(cluster id)

	
	local mMin = 0
	nlcom   (eq1:(([gain]_cons + `mMin'*[gain]win) / (([gain]_cons + `mMin'*[gain]win) - ([loss]_cons +  `mMin'*[loss]win))) ///
			- ([gain]win / ([gain]win - [loss]win))) ///
			(eq2:([gain]win / ([gain]win - [loss]win))) ///						
			, post		


	cap postclose welfareTable
	postfile welfareTable ///
			 aG str3 starsGainA double aL str3 starsLossA ///
			 bG str3 starsGain double bL str3 starsLoss ///
			 double alphaMin str4 starsAlpha ///
			 double alphaMax str4 starsAlphaMax ///
			 str4 cond double p ///
			 using "data/welfareTable.dta", replace
	
	foreach cond2 of numlist 1/6 13/18 {
		preserve 
			
			keep if known == 1
			keep if cond2 == `cond2'
			set seed 1
			
			mysureg (gain win) /// 
					(loss win) ///
				, vce(cluster id)
		
			
			local mMin = 0.5
			local mMax = 5.5
			
			// Condition
			nlcom   (eq1:(([gain]_cons + `mMin'*[gain]win) / (([gain]_cons + `mMin'*[gain]win) - ([loss]_cons +  `mMin'*[loss]win))) ///
					- ([gain]_cons + `mMax'*[gain]win) / (([gain]_cons + `mMax'*[gain]win) - ([loss]_cons +  `mMax'*[loss]win))) ///
					(eq2: (([gain]_cons + `mMax'*[gain]win) / (([gain]_cons + `mMax'*[gain]win) - ([loss]_cons +  `mMax'*[loss]win)))) ///	
					(eq3: ([gain]_cons + `mMin'*[gain]win)) ///
					(eq4: ([loss]_cons + `mMin'*[loss]win)) ///
					(eq5: (([gain]_cons + `mMin'*[gain]win) / (([gain]_cons + `mMin'*[gain]win) - ([loss]_cons +  `mMin'*[loss]win)))) ///
					(eq6: ([gain]_cons + `mMax'*[gain]win)) ///
					(eq7: ([loss]_cons + `mMax'*[loss]win)) ///
				, post
			
			
			// New
			local bG = _b[eq6]
			local bL = _b[eq7]
			local seBg = _se[eq6] 
			local seBl = _se[eq7]
			
			test eq6 == 0
			local pGain = r(p)
			test eq7 == 0
			local pLoss = r(p)
			
			
			local starsGain = ""
			if (`pGain' <= 0.01) {
				local starsGain = "***"
			}
			if (`pGain' <= 0.05 & `pGain'>0.01) {
				local starsGain = "**"
			}
			if (`pGain' < 0.1 & `pGain'>0.05) {
				local starsGain = "*"
			}
			
			
			local starsLoss = ""
			if (`pLoss' <= 0.01) {
				local starsLoss = "***"
			}
			if (`pLoss' <= 0.05 & `pLoss'>0.01) {
				local starsLoss = "**"
			}
			if (`pLoss' < 0.1 & `pLoss'>0.05) {
				local starsLoss = "*"
			}
			
			
			
			
			// Previous
			
			local alphaMin = _b[eq2]
			local alphaSE = _se[eq2]
			local alphaMax = _b[eq5]
			local alphaMaxSE = _se[eq5]
			
			test eq2 == 0
			local pAlpha = r(p)
			
			local starsAlpha = ""
			if (`pAlpha' <= 0.01) {
				local starsAlpha = "***"
			}
			if (`pAlpha' <= 0.05 & `pAlpha'>0.01) {
				local starsAlpha = "**"
			}
			if (`pAlpha' < 0.1 & `pAlpha'>0.05) {
				local starsAlpha = "*"
			}

			local possibilityCond = "No"
			if (_b[eq1] >= 0) {
				local possibilityCond = "Yes"
			}
			
			
			test eq5 == 1
			local pAlphaMax = r(p)
			
			local starsAlphaMax = ""
			if (`pAlphaMax' <= 0.01) {
				local starsAlphaMax = "***"
			}
			if (`pAlphaMax' <= 0.05 & `pAlpha'>0.01) {
				local starsAlphaMax = "**"
			}
			if (`pAlphaMax' < 0.1 & `pAlpha'>0.05) {
				local starsAlphaMax = "*"
			}

			local possibilityCond = "No"
			if (_b[eq1] >= 0) {
				local possibilityCond = "Yes"
			}
			
			
			local aG = _b[eq3]
			local aGse = _se[eq3]
			local aL = _b[eq4]
			local aLse = _se[eq4]
			
			
			
			test _b[eq3] == 0
			local pGainA = r(p)
			test _b[eq4] == 0
			local pLossA = r(p)
			
			
			local starsGainA = ""
			if (`pGainA' <= 0.01) {
				local starsGainA = "***"
			}
			if (`pGainA' <= 0.05 & `pGainA'>0.01) {
				local starsGainA = "**"
			}
			if (`pGainA' < 0.1 & `pGainA'>0.05) {
				local starsGainA = "*"
			}
			
			
			local starsLossA = ""
			if (`pLossA' <= 0.01) {
				local starsLossA = "***"
			}
			if (`pLossA' <= 0.05 & `pLossA'>0.01) {
				local starsLossA = "**"
			}
			if (`pLossA' < 0.1 & `pLossA'>0.05) {
				local starsLossA = "*"
			}
			
			
			noisily: di "`possibilityCond'"
					
			test eq1
			local pTest = r(p)

			
		restore
		post welfareTable ///
			(`aG') ("`starsGainA'") (`aL') ("`starsLossA'")  ///
			(`bG') ("`starsGain'") (`bL') ("`starsLoss'") ///
			(`alphaMin') ("`starsAlpha'") (`alphaMax') ("`starsAlphaMax'") ///
			("`possibilityCond'") (`pTest')
						
		post welfareTable ///
			(`aGse') ("") (`aLse') ("") ///
			(`seBg') ("") (`seBl') ("") ///
			(`alphaSE') ("") (`alphaMaxSE') ("") ("") (.)
		
		
	}
	postclose welfareTable
	
	// Format the table (Supplementary Table C.1)
	preserve
		clear
		use "data/welfareTable.dta"
						
		gen c1 = "("
		replace c1 = "" if mod(_n,2) == 1
		
		gen c2 = ") & ("
		replace c2 = "  &  " if mod(_n,2) == 1
		
		gen c3 = c2
		gen c4 = c2
		gen c5 = c2
		gen c6 = c2
		
		gen c7 = ") & "
		replace c7 = "  &  " if mod(_n,2) == 1
		
		gen c8 = " & " 
		
		gen c9 = " \\ "
		
		order c1 aG starsGainA c2 /// 
				 aL starsLossA c3 ///
				 alphaMax starsAlphaMax c6 ///
				 bG starsGain  c4 /// 
				 bL starsLoss  c5 /// 
				 alphaMin starsAlpha c7 ///
				 cond c8 p c9
		
		
		// Input format of the table
		
		
		/*
		Condition
		Delay
		Noncompensable
		80%
		50%
		20%
		Compensable 
		80%
		50%
		20%

		Contemporaneous
		Noncompensable
		80%
		50%
		20%
		Compensable 
		80%
		50%
		20%
		*/
		
		
		
		
		gen line = (_n+1)/2
		
		gen newline = .
		replace newline = line - 9 	if line >= 10
		replace newline = line   	if line >= 4 & line < 7
		replace newline = line   	if line >= 7 & line < 10
		replace newline = line + 9 	if line < 4

		
		replace line = newline 
		sort line 
		drop line newline
		
		
		gen rowlabel = "&"
		replace rowlabel = "\qquad 20\% & " if mod(_n, 6) == 1
		replace rowlabel = "\qquad 50\% & " if mod(_n, 6) == 3
		replace rowlabel = "\qquad 80\% & " if mod(_n, 6) == 5
		
		local lines = _N
		local newLines = `lines' + 6
		di `newLines'
		set obs `newLines'
		
		
		
		replace rowlabel = "Limited Loss (Grid First) \\" 				if _n == 25
		replace rowlabel = "\quad Contemporaneous \\ \qquad Prior success probability\\" 	if _n == 26
		replace rowlabel = "\quad Delayed \\ \qquad Prior success probability\\" 		if _n == 27
		replace rowlabel = "Large Loss \\ \qquad (Incentive First) \\" 	if _n == 28
		replace rowlabel = "\quad Contemporaneous \\ \qquad Prior success probability\\" 	if _n == 29
		replace rowlabel = "\quad Delayed \\ \qquad Prior success probability\\" 		if _n == 30
		
		
		gen location = _n
		replace location = 0 		if location == 25
		replace location = 0.5 		if location == 26
		replace location = 6.5 		if location == 27
		replace location = 12.5 	if location == 28
		replace location = 12.6 	if location == 29
		replace location = 18.5 	if location == 30
		
		sort location
		drop location
		order rowlabel
		
		
		format aG aL bG bL alphaMin alphaMax p %9.3f
		export delimited using "../writeup/tables/SupplementaryTableC1.tsv", delimiter(tab)  datafmt novar replace
						
	restore		
}


		